package com.asdhawaii.otello.players;

import java.util.ArrayList;
import java.util.List;
import com.asdhawaii.otello.board.Move;
import com.asdhawaii.otello.board.Square;
import com.asdhawaii.otello.board.SquareContents;

/**
 * A mid-level Otello AI that focuses on minimizing the number of pieces taken.
 * 
 * This strategy seems counter-intuitive, possibly because it is.  The objective of the game is to
 * finish with the most pieces, so the natural instinct is to take as many pieces as possible (as
 * is the case for Level1Ai).  However, always taking more pieces means that there are fewer enemy
 * pieces and thus fewer possible moves.  Fewer available moves makes it more likely that the 
 * player would be forced to give away a corner to the opponent, which is not good.  Therefore, 
 * this AI will use the opposite approach and take as few pieces as possible, thus increasing the 
 * number of moves available.  
 * 
 * This AI still focuses on corners though, so taking a corner is still a priority as is avoiding
 * moves that would give the corner away to the opponent.
 * 
 * Of course, if the objective is to keep the most possible moves available, then it would make
 * more sense to actually choose the move that results in the most moves for the next turn; that is
 * what higher-level AIs should do.
 * 
 * @author Branden Ogata
 * 
 */

public class Level3Ai extends AbstractOtelloAi
{
  /**
   * Creates a new Level3Ai.
   * 
   * @param color The SquareContents indicating the color of the pieces for the new Level3Ai.
   * 
   */
  
  public Level3Ai(SquareContents color)
  {
    super(color);
  }

  /**
   * Returns the move or one of the moves that this AI finds the best out of the available moves.
   * 
   * @param board The Square[][] representing the Otello board.
   * @param moves The List<Move> containing the valid moves on the board.
   * 
   * @return selection A Move representing the move that this Level3Ai chose.
   * 
   */
  
  @Override
  public Move getMove(Square[][] board, List<Move> moves)
  {
    List<Move> candidates = new ArrayList<Move>();
    List<Move> corners = AiStrategies.getCorners(board, moves, this.getColor());
    List<Move> contiguousC = AiStrategies.getContiguousCSquares(board, moves, this.getColor());
    List<Move> contiguousX = AiStrategies.getContiguousXSquares(board, moves, this.getColor());
    List<Move> aSquares = AiStrategies.getASquares(board, moves, this.getColor());
    List<Move> cSquares = AiStrategies.getCSquares(board, moves, this.getColor());
    List<Move> xSquares = AiStrategies.getXSquares(board, moves, this.getColor());
        
    // If there are corners available
    if (!corners.isEmpty())
    {
      candidates.addAll(corners);
    }
    
    // Else if there are squares adjacent to own corners available
    else if (!contiguousC.isEmpty())
    {      
      candidates.addAll(contiguousC);
    }
    else if (!contiguousX.isEmpty())
    {      
      candidates.addAll(contiguousX);
    }
    
    // Else if there are A squares available
    else if (!aSquares.isEmpty())
    {
      candidates.addAll(aSquares);
    }
    
    // Else remove C, X squares from candidates before getting move of Level1Ai
    else
    {
      candidates.addAll(moves);
      candidates.removeAll(cSquares);
      candidates.removeAll(xSquares);
      
      // If no valid moves with that restriction, then prefer C over X squares
      if (candidates.isEmpty())
      {
        candidates.addAll(cSquares);
        
        if (candidates.isEmpty())
        {
          candidates.addAll(xSquares);
        }
      }
    }
     
    return AiStrategies.random(AiStrategies.minima(board, candidates, this.getColor()));
  }
}
